      1  import java.sql.Connection;

      2  import java.sql.ResultSet;

      3  import java.sql.ResultSetMetaData;

      4  import java.sql.Statement;

      5  import java.sql.SQLException;

      6  import java.io.File;

      7  import java.io.IOException;

      8  import java.util.Scanner;

      9

     10  /**

     11     Executes all SQL statements from a file or the console.

     12  */

     13  public class ExecSQL

     14  {

     15     public static void main(String[] args)

     16           throws SQLException, IOException, ClassNotFoundException

     17     {

     18        if (args.length == 0)

     19        {

     20           System.out.println(

     21                 "Usage: java -classpath driver_class_path"

     22                 + File.pathSeparator

     23                 + ". ExecSQL propertiesFile [SQLcommandFile]");

     24           return;

     25        }

     26

     27        SimpleDataSource.init(args[0]);

     28

     29        Scanner in;

     30        if (args.length > 1)

     31        {

     32           in = new Scanner(new File(args[1]));

     33        }

     34        else

     35        {

     36           in = new Scanner(System.in);

     37        }

     38

     39        Connection conn = SimpleDataSource.getConnection();

     40        try

     41        {

     42           Statement stat = conn.createStatement();

     43           while (in.hasNextLine())

     44           {

     45              String line = in.nextLine();

     46              try

     47              {

     48                 boolean hasResultSet = stat.execute(line);

     49                 if (hasResultSet)

     50                 {

     51                    ResultSet result = stat.getResultSet();

     52                    showResultSet(result);

     53                    result.close();

     54                 }

     55              }

     56              catch (SQLException ex)

     57              {

     58                 System.out.println(ex);

     59              }

     60           }

     61        }

     62        finally

     63        {

     64           conn.close();

     65        }

     66     }

     67

     68     /**

     69        Prints a result set.

     70        @param result the result set

     71     */

     72     public static void showResultSet(ResultSet result)

     73           throws SQLException

     74     {

     75        ResultSetMetaData metaData = result.getMetaData();

     76        int columnCount = metaData.getColumnCount();

     77

     78        for (int i = 1; i <= columnCount; i++)

     79        {

     80           if (i > 1) { System.out.print(", "); }

     81           System.out.print(metaData.getColumnLabel(i));

     82        }

     83        System.out.println();

     84

     85        while (result.next())

     86        {

     87           for (int i = 1; i <= columnCount; i++)

     88           {

     89              if (i > 1) { System.out.print(", "); }

     90              System.out.print(result.getString(i));

     91           }

     92           System.out.println();

     93        }

     94     }

     95  }